package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.FileModel;
import com.ruoyi.system.domain.FileLibraryModel;
import com.ruoyi.system.mapper.FileLibraryMapper;
import com.ruoyi.system.service.FileLibraryService;
import com.ruoyi.system.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：Wangziyi
 * @date ：Created in 2023/12/12 10:12
 * @description：ToDo
 * @modified By：
 * @version: 1.0
 */
@Service
public class FileLibraryServiceImpl extends ServiceImpl<FileLibraryMapper, FileLibraryModel> implements FileLibraryService {

    @Autowired
    private FileService fileService;


    @Override
    public Boolean logicRemoveFileLibrary(Long id) {

        //判断一下这个知识库是不是自己的
        LambdaQueryWrapper<FileLibraryModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileLibraryModel::getId,id);
        List<FileLibraryModel> list = this.list(queryWrapper);
        if (list == null || list.size() == 0){
            throw new ServiceException("该知识库不存在！");
        }

        if (list.get(0).getUserId() != SecurityUtils.getUserId()){
            throw new ServiceException("不能删除该知识库！");
        }

        //删除逻辑
        LambdaUpdateWrapper<FileLibraryModel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(FileLibraryModel::getIsDeleted,1).eq(FileLibraryModel::getId,id);
        boolean res = this.update(updateWrapper);

        LambdaUpdateWrapper<FileModel> fileUpdateWrapper = new LambdaUpdateWrapper<>();
        fileUpdateWrapper.set(FileModel::getIsDeleted,1).eq(FileModel::getLibraryId,id);
        boolean fileRes = fileService.update(fileUpdateWrapper);

        return res;
    }
}
